/**
** 兴趣组件 渲染的全部逻辑
**/
define(['../model/data','../common/service',
        '../business/recommend','../common/service',
        '../plugins/dialog'],function(DATA,http,recom, service, dialog){
    return function(node,index,data){
		var cb = arguments.callee;
        var id = "Interests";
        if(!data) data = [];
        var daIndex = data.length?(function(){for(var i=0;i<DATA.langs.length;i++){ if(DATA.langs[i].value==(data[0].locale)) return i; }}())||0:0;
		var locale = (DATA.langs && DATA.langs[daIndex].value)||null;
		if(!locale){
			setTimeout(function(){
				cb(node,index,data);
			});
			return;
		}
		node = node||$("#"+id+" .user-body>li:first");

        initNode();
        //初始化语言下拉列表
        initLang();
        //根据语言渲染兴趣类别
        initBrowse(locale);
        //初始化查询兴趣词
        initSearchArea();
        //初始化事件
        initEvent();
        //回填数据
        fillCache();

        /**
         * 初始化语言
         */
        function initLang(){
            //兴趣   语言
            node.find("div.drop-lang").empty().bootDrop({
                list:DATA.langs,
                prop:"name",
                value:daIndex
            }).on("selectChanged",function(e,o){
                locale = o.data.value;
                //kit.addBorderGreen($(e.target).parents('.alert-dismissible').parent());
                //每次切换语言重新渲染联想区域
                initBrowse(locale);
            });
        }

        /**
         * 初始化当前Node
         */
        function initNode(){
            node.find(".intsearch").empty().html('<input type="hidden" choiced-interests>\
                <div class="search hide" search><span style="padding-left: 10px;line-height:30px"><span class="glyphicon glyphicon-search"></span>'+I18N.getText('createAd','interestSearch')+'</span>\
            <ul class="search-list list-element">\
                </ul>\
                </div>');
            node.find(".browse").empty().html('<span style="padding-left: 10px;line-height:30px"><span class="glyphicon glyphicon-thumbs-up">\
            </span>'+I18N.getText('createAd','recommendedClassification')+'</span>\
            <ul class="shortcut-drop">\
                </ul>');
            node.find(".suggession").empty().html('<span style="padding-left: 10px;line-height:30px"><span class="glyphicon glyphicon-question-sign"></span>'+I18N.getText('createAd','interestAssociation')+'</span>\
                <ul class="suggession-list">\
                </ul>');
        }

        /**
         *按语言重新渲染联想区域
         */
        function initBrowse(_locale){
            var ta = node.find("ul.shortcut-drop:first");
            ta.empty();
            node.find(".interest-browse").addClass("no-use");
            node.find(".interest-browse").attr('title', I18N.getText('createAd', 'beingLoaded'));
            var param = DATA.body([{key:"locale", value:_locale}]);
            http.do("/search/searchInterestsCategory",param,"GET").then(function(result){
                if(typeof(result)=="string") result = JSON.parse(result);
                if (!result.data || 0 == result.data.length){
                    node.find(".interest-browse").attr('title', I18N.getText('createAd', 'noRecommendationInformation'));
                } else {
                    node.find(".interest-browse").removeClass("no-use");
                    node.find(".interest-browse").attr('title',  I18N.getText('createAd', 'recommendation'));
                    var _browseList = renderBrowseList(result.data, locale, false);
                    ta.append(_browseList);
                    interestCategoryParentEvent();
                }
            },function(err){});
        }

        /**
         *渲染兴趣类别区域
         */
        function renderBrowseList(_interestCategories, _locale, isChildren){
            var _interestsInput =  node.find('[data-type=interests]');
            var choicedInterest = _interestsInput.data("choicedInterest") || {};
            var children;
            var _t = '<ul class="list-element' + (isChildren ? ' list-element-children' : ' list-element-first') +  '">';
            $.each(_interestCategories, function(index, _interestCategory){
                children = _interestCategory.son;
                if (children){
                    _t += '<li>';
                    //有子则有折叠按钮
                    _t += '<span class="interest-parent"><span class="ts-icon  fa fa-caret-right" style="width:26px;"></span>';
                    _t +=  '<span class="ts-title" style="padding-left:26px" node-id="'
                        + _interestCategory.id + '" title="'
                        + _interestCategory.name + '">' + _interestCategory.name
                        + '<span class="pull-right" style="padding-right: 40px;">' + kit.toThousands(_interestCategory.audienceSize || 0) + '</span></span>'
                        + '</span>';
                    _t += renderBrowseList(children, _locale, true);
                } else {
                    _t += '<li class="ts-title choices-action category' +  (choicedInterest[_interestCategory.id] ? ' is-checked' : '') + '" node-id="'
                        + _interestCategory.id + '" title="'
                        + _interestCategory.name + '" interest-id="'+ _interestCategory.id
                        + '" interest-name="' + _interestCategory.name + '" locale="' + _locale + '">';
                    _t +=  '<span  style="padding-left:26px">' + _interestCategory.name
                        +  '<span class="pull-right" style="padding-right: 40px;">' + kit.toThousands(_interestCategory.audienceSize || 0) + '</span></span>';
                }

                _t += '</li>';
            });
            _t += '</ul>';
            return _t;
        }

        //绑定兴趣类别展开折叠事件
        function interestCategoryParentEvent(){
            var _interestParent = node.find('.interest-parent');
            _interestParent.off("click");
            _interestParent.on("click", function(){
                var _interestParentE = $(this);
                var _interestParentIcon = $(this).find(".ts-icon");
                var _list = _interestParentE.parent().find('> .list-element');
                if (_interestParentIcon.hasClass("fa-caret-right")){
                    //展开
                    _interestParentIcon.removeClass("fa-caret-right");
                    _interestParentIcon.addClass("fa-caret-down");
                    _list.slideDown(300);
                } else {
                    //折叠
                    _interestParentIcon.removeClass("fa-caret-down");
                    _interestParentIcon.addClass("fa-caret-right");
                    _list.slideUp(300);
                }
            });
        }

        function initEvent(){
            //绑定显示兴趣类别事件
            showCategoriesEvent();
            //保存兴趣事件
            saveEvent();
            //导入兴趣事件
            importEvent();
            //绑定兴趣词选中事件：选中类别、联想以及搜索出的兴趣词
            selectEvent();
            //点击其他区域 兴趣搜素恢复初始状态
            recoverInterestAreaEvent();
        }

        /**
         * 绑定显示兴趣类别事件
         */
        function showCategoriesEvent(){
            //手型被点击之后 展现
            node.find(".interest-browse").unbind("click").click(function(e){
                e.stopImmediatePropagation();
                if ($(this).hasClass("no-use")){
                    return false;
                }
                $('.drop-lang').removeClass('open');
                node.find("div[browse]").removeClass("hide");
                node.find("[data-type='interests']").val('');

                //隐藏查询结果区域
                node.find("div[search]").addClass("hide");

                //显示推荐的类别
                node.find("div[browse]").removeClass("hide");

                //显示联想区域
                if (node.find('div[suggession]').find(".choices-action").length > 0){
                    node.find('div[suggession]').removeClass("hide");
                }
            });
        }

        /**
        *
        ** 弹出窗口,保存兴趣
        **/
        function saveEvent(){
            node.find("button.save-int").unbind("click").on("click",function(){
                var currentIndex = node.attr("index");
                if(DATA.nowTemp.Interests[currentIndex] && DATA.nowTemp.Interests[currentIndex].length > 0){
                    var self = this;
                    $("body>div.modal.common-modal").dialog({
                        id:$(this).data("target").replace("#",""),
                        label:$(this).data("label"),
                        title: I18N.getText('createAd', 'saveInterest')
                    }).unbind("ok").on("ok",function(e,o){
                        var body = DATA.body([{key:"name","value":o.name},
                            {key:"type",value:"Interests"},
                            {key:"spec",value:JSON.stringify(DATA.nowTemp.Interests[currentIndex])},
                            {key:"fbConnectObjectId",value:DATA.app.fbConnectionObjectId},
                            {key:"productConfigurationId",value:DATA.prod.id}]);
                        http.do("/targetingspectemplates/createTargetingSpecTemplate",body).then(function(result){
                            if(result.flag=="fail"){
                                if (96 == result.errorCode){
                                    kit.alert2(I18N.getText('createAd', 'interestTemplateRepetition'));
                                } else {
                                    kit.alert2(result.msg);
                                }

                            }
                        },function(err){});
                    });

                    $("body>div.modal.common-modal").find("label>input[type]").val('');
                } else {
                    kit.warning(node.find('.int-drop-wrapper'));
                    return false;
                }
            });
        }

        /**
         * 弹出窗口，导入兴趣
         */
        function importEvent(){
            node.find("button.import-int").unbind("click").on("click", function(){
                $("body>div.modal.common-modal").dialog({
                    id:$(this).data("target").replace("#",""),
                    label:$(this).data("label"),
                    title:I18N.getText('createAd', 'importInterest'),
                    body:'<div class="intesret-int">\
                  <div class="interest-int-title">'+ I18N.getText('createAd', 'tmplList') +'</div>\
                  <div class="interest-int-content">\
                      <div class="input-drop dropdown saved-temps"></div>\
                  </div>\
                </div>\
                <div class="intesret-int">\
                  <div class="interest-int-title">'+ I18N.getText('createAd', 'selectedInterest') +'</div>\
                  <div class="interest-int-content">\
                      <select class="form-control choosed-ints" multiple></select>\
                  </div>\
                </div>'
                }).unbind("ok").on("ok",function(e,o){
                    if (!o.importInts || 0 == o.importInts.length){
                        return false;
                    }

                    //对待导入的兴趣词进行校验
                    var needInterestValid = [];
                    $.each(o.importInts, function(i, _importInt){
                        needInterestValid.push({
                            id : _importInt.id,
                            name : _importInt.name,
                            locale : _importInt.locale
                        })
                    });

                    http.do("/search/adInterestValid",{param : JSON.stringify(needInterestValid)}).then(function(result){
                        if(typeof(result)=="string") result = JSON.parse(result);
                        var da = result.data;
                        if (!da || 0 == da.length){
                            return false;
                        }

                        //渲染到选中区域
                        $.each (da, function(i, _validInterest){
                            //当前兴趣词尚未被选择则显示
                            choiceInterest(_validInterest.id, _validInterest.name, _validInterest.locale || 'en_US', true);
                        });
                    },function(err){});
                });

                var body = DATA.body([{key:"type",value:"Interests"},
                    {key:"fbConnectObjectId",value:DATA.app.fbConnectionObjectId},
                    {key:"productConfigurationId",value:DATA.prod.id}]);
                http.do("/targetingspectemplates/getTargetingSpecTemplatesByType",body).then(function(result){
                    if(typeof(result)=="string") result = JSON.parse(result);
                    var da = result.data;
                    DATA.importTemp = da[0];
                    $("div.saved-temps").empty().bootDrop({
                        list:da,
                        prop:"name",
                        value:0
                    }).on("selectChanged",function(e,o){
                        DATA.importTemp = o.data;
                        DATA.importTempIds = [];
                        $("select.choosed-ints").empty().select2({
                            data:$.map(JSON.parse(DATA.importTemp.spec),function(item){
                                item.text = item.name;
                                DATA.importTempIds.push(item.id);
                                return item;
                            })
                        }).select2("val",DATA.importTempIds);
                    });

                    //没有兴趣
                    if(!DATA.importTemp){
                        $("select.choosed-ints").empty().select2();
                    } else {
                        console.log(JSON.parse(DATA.importTemp.spec))
                        DATA.importTempIds = [];
                        $("select.choosed-ints").empty().select2({
                            data:$.map(JSON.parse(DATA.importTemp.spec),function(item){
                                item.text = item.name;
                                DATA.importTempIds.push(item.id);
                                return item;
                            })
                        }).select2("val",DATA.importTempIds);
                    }
                },function(err){});
            });
        }

        function initSearchArea(){
            //渲染查询区域
            renderSearchArea();
            //绑定兴趣输入框获取光标事件
            searchInputFocus();
            //兴趣词搜索事件：输入、复制、回退
            searchEvent();
            //删除选中的兴趣词
            deleteEvent();
            //动态渲染输入框宽度
            //dynamicWidth();
        }

        function renderSearchArea(){
            var _choicedInterests = node.find("input[choiced-interests]");
            _choicedInterests.after('<div class="choices-area" style="min-height: 32px;">\
            <i class="icon-loading fa fa-refresh" style="right: 5px; top: 9px;"></i>\
                <ul class="choices-list">\
                <li class="search-field">\
                <input type="text" data-type="interests" class="search-input" record="" placeholder="'+I18N.getText('createAd', 'searchInterest')+'...">\
                </li>\
                </ul>\
                </div>');
        }

        /**
         * 绑定兴趣词搜索事件：输入、复制、回退
         */
        function searchEvent(){
            //绑定批量粘贴功能
            var isCopy		= false,		 //用于区分当前是批量粘贴还是单个输入
                isCtrlKey   = false,		 //用于处理copy监控偶发性失效问题
                isLoading   = false;		 //用于处理重复粘贴

            //触发时间在select2.query之前
            var _interestsInput = node.find("[data-type='interests']");
            _interestsInput.unbind('keydown');
            _interestsInput.keydown(function(e){
                var key = e.which;
                if (key === 8 && $(this).val() === '') {
                    //回退且输入内容为空
                    var $previousChoice = $(this).parent().prev('.choice-field');
                    if ($previousChoice.length > 0){
                        $previousChoice.find('.del-action').trigger("click");
                    }
                    e.preventDefault();
                    //var $previousChoice = self.$searchContainer
                    //    .prev('.select2-selection__choice');
                    //
                    //if ($previousChoice.length > 0) {
                    //    var item = $previousChoice.data('data');
                    //
                    //    self.searchRemoveChoice(item);
                    //    evt.preventDefault();
                    //}

                    return false;
                }

                if(e.ctrlKey){
                    isCtrlKey = true;
                    window.setTimeout(function(){
                        isCtrlKey = false;
                    },500);
                }

                if(isLoading){
                    return false;
                }

                if(isCtrlKey && e.keyCode == 86){
                    isCopy = true;
                    node.find("[data-type='interests']").val('');
                }
            });

            var _newQ;
            _interestsInput.unbind('keyup');
            _interestsInput.keyup(function(e){
                if (node.find("[data-type='interests']").hasClass("copy") || isLoading){
                    return;
                }
                var choicedInterest = _interestsInput.data("choicedInterest") || {};
                if (isCopy){
                    if (isLoading){
                        return;
                    }
                    //隐藏大类
                    node.find("div[browse]").addClass("hide");
                    _interestsInput.addClass("copy");

                    node.find("div[search]").addClass("hide");
                    kit.loading.show(node.find(".alert"));
                    isLoading = true;
                    var param = DATA.body([{key:"interestList", value:_interestsInput.val() || ''},{key:"locale", value:locale}]);
                    http.do("/search/searchInterestList",param).then(function(result){
                        if(typeof(result)=="string") result = JSON.parse(result);
                        var da = result.data;
                        isLoading = false;
                        isCopy = false;
                        _interestsInput.val('');
                        _interestsInput.removeClass("copy");
                        kit.loading.hide(node.find(".alert"));

                        if(!da || 0 == da.length ){
                            return false;
                        }

                        $.each(da,function(i,v){
                            //当前兴趣词尚未被选择则显示
                            choiceInterest(v.id, v.name, locale, true)
                        });

                        node.find('div[suggession]').addClass("hide");
                        node.find('div[suggession]').find(".suggession-list").empty();
                    },function(err){});
                } else {
                    var _interestsSearch = node.find(".search-list");
                    var _interestsInputValue = $.trim($(this).val());
                    _newQ = _interestsInputValue;
                    if ('' == _interestsInputValue){
                        _interestsSearch.empty();
                        _interestsSearch.parent().addClass("hide");
                        return;
                    }
                    //隐藏大类
                    node.find("div[browse]").addClass("hide");
                    //1、隐藏并清空兴趣搜索结果区域
                    _interestsSearch.html('<li class="search-warning">'+I18N.getText('common', 'searching')+'...</li>');
                    _interestsSearch.parent().removeClass("hide");

                    window.setTimeout(function(){
                        if (_interestsInputValue != _newQ){
                            return;
                        }
                        //根据输入搜索兴趣词
                        var param = DATA.body([{key:"type", value:"adinterest"},{key:"q", value:_interestsInputValue},{key:"locale", value:locale}]);
                        http.do("/search/searchInterests",param).then(function(result){
                            if (_newQ != JSON.parse(param.param).q) {
                                return false;
                            }
                            if(typeof(result)=="string") result = JSON.parse(result);
                            var da = result.data;
                            var _searchHtml = '';
                            if (da && 0 < da.length){
                                var name;
                                $.each(da, function(i, searchResult){
                                    //格式化人数
                                    searchResult.audienceSize = kit.toThousands(searchResult.audienceSize || 0);

                                    if (choicedInterest[searchResult.id]){
                                        searchResult.isHide = true;
                                    }

                                    name = searchResult.name;
                                    var _indexOfNum = name.toLowerCase().indexOf(_newQ.toLowerCase());
                                    if(_indexOfNum != -1){
                                        name =
                                            name.slice(0,_indexOfNum)
                                            + '<span class="search-text">'
                                            + name.slice(_indexOfNum, _indexOfNum + _newQ.length)
                                            + '</span>'
                                            + name.slice(_indexOfNum + _newQ.length)
                                    }
                                    searchResult.showName = name;
                                });


                                $.each(da, function(i, searchResult){
                                    if (searchResult.isHide){
                                        _searchHtml += '<li class="ts-title choices-action is-checked" node-id="' + searchResult.id + '" title="'+ searchResult.name + '">\
                                        <span  style="padding-left:52px" >' +  searchResult.showName +
                                            '<span class="pull-right" style="padding-right: 40px;">' + searchResult.audienceSize + '</span>\
                                        </span>\
                                        </li>';
                                    }else{
                                        _searchHtml += '<li class="ts-title choices-action" node-id="' + searchResult.id + '" title="' + searchResult.name + '">\
                                        <span  style="padding-left:52px" >' + searchResult.showName +
                                            '<span class="pull-right" style="padding-right: 40px;">' + searchResult.audienceSize + '</span>\
                                        </span>\
                                        </li>';
                                    }
                                });

                            } else {
                                _searchHtml += '<li class="search-warning">'+I18N.getText('common', 'resultEmpty')+'...</li>'
                            }
                            _interestsSearch.html(_searchHtml);
                            //选中兴趣词事件
                            //choiceInterestEvent();
                        },function(err){});
                    },100);
                }
            });
        }

        /**
         * 选中兴趣词
         */
        function choiceInterest(interestId, interestName, locale, isCache){
            var _interestsInput =  node.find('[data-type=interests]');
            var choicedInterest = _interestsInput.data("choicedInterest") || {};
            if (choicedInterest[interestId]){
                return false;
            }
            choicedInterest[interestId] = {
                id : interestId,
                name : interestName,
                locale : locale
            };
            _interestsInput.data("choicedInterest", choicedInterest);

            //选中之后不可选
            var allChoicesActionE = node.find('.choices-action[node-id="' + interestId + '"]');
            allChoicesActionE.addClass('is-checked');

            //存储name至展示区域
            var _LastSelected = node.find(".choices-list").find(".last-selected");
            _LastSelected.removeClass("last-selected");
            var _showHtml = '<li class="choice-field last-selected" node-id="'+ interestId
                + '" interest-name="' + interestName + '" locale="' + locale + '">'
                + '<span class="del-action">×</span>'
                + '<span class="show-text" title="'+ interestName +'">'
                + interestName
                + '</span>'
                + '</li>';
            _interestsInput.parent().before(_showHtml);
            //动态渲染宽度
            var _searchField 	= node.find('.search-field');
            var _choicesList 	= node.find('.choices-list');
            _searchField.width(80);
            var _w  = _choicesList.width()
                - _searchField.get(0).offsetLeft
                - 2;
            if (80 > _w){
                _searchField.width('100%');
            } else {
                _searchField.width(_w);
            }


            if (isCache){
                var newInterest = {id:interestId,name:interestName,locale:locale};
                data.push(newInterest);
                var _currentIndex = node.attr("index");
                DATA.update(id,_currentIndex,data);//添加
            }
        }

        /**
         * 绑定删除兴趣词事件
         */
        function deleteEvent(){
            node.off('click', '.del-action');
            node.on('click', '.del-action', function(e){
                var _interestsInput =  node.find('[data-type=interests]');
                var choicedInterest = _interestsInput.data("choicedInterest") || {};
                var _delctionE = $(this);
                var _delForLi = _delctionE.parent();
                var _nodeId = _delForLi.attr("node-id");
                node.find('.choices-action[node-id="' + _nodeId + '"]').removeClass('is-checked');
                node.find('.choices-action[node-id="' + _nodeId + '"]').removeClass('hide');
                delete choicedInterest[_nodeId];
                _interestsInput.data("choicedInterest", choicedInterest);
                //清除展示
                var isLast = _delForLi.hasClass("last-selected");
                if (isLast){
                    //如果删除的是最后一个兴趣词 则清除相应的联想
                    node.find('div[suggession]').addClass("hide");
                    node.find("ul.suggession-list").empty();
                }
                _delForLi.remove();
                //动态渲染宽度
                var _searchField 	= node.find('.search-field');
                var _choicesList 	= node.find('.choices-list');
                _searchField.width(80);
                var _w  = _choicesList.width()
                    - _searchField.get(0).offsetLeft
                    - 2;
                if (80 > _w){
                    _searchField.width('100%');
                } else {
                    _searchField.width(_w);
                }

                //删除缓存中的数据
                var currentIndex = node.attr("index");
                var da = DATA.nowTemp[id][currentIndex];
                var o = {
                    id : _nodeId
                };
                var inx = kit.objectIn(da,o,"id","id");
                data.splice(inx,1);
                DATA.update(id,currentIndex,data);
            });
        }

        /**
         * 绑定兴趣输入框获取光标事件
         */
        function searchInputFocus(){
            var _interestsInput =  node.find('[data-type=interests]');
            _interestsInput.unbind('focus');
            _interestsInput.focus(function(e){
                $(this).parents('.choices-area').addClass('seach-active');
                //$('div[interest]', $box).addClass('interest-active');
                var _interestsInputValue = $.trim($(this).val());
                if (''!= _interestsInputValue){
                    return;
                }

                //3、兴趣输入框获取光标时显示兴趣联想词
                //显示联想区域
                if (node.find('div[suggession]').find(".choices-action").length > 0){
                    node.find('div[suggession]').removeClass("hide");
                }
            });
        }

        /**
         * 绑定兴趣词选中事件：选中类别、联想以及搜索出的兴趣词
         */
        function selectEvent(){
            node.off('mousedown', '.choices-action');
            node.on('mousedown', '.choices-action', function(e){
                e.stopImmediatePropagation();
                if ($(this).hasClass("is-checked")){
                    return false;
                }
                $(this).addClass("is-checked");
                var _interest = $(this).attr("title");
                var _interestId = $(this).attr("node-id");
                $('.search-input', $(this).closest('.intsearch')).val('');
                //选中兴趣词
                choiceInterest(_interestId, _interest, locale, true)

                //重新联想
                suggestion(_interest, locale);
            });
        }

        function dynamicWidth(){
            //1、打开下拉列表事件
            node.off('click', '.choices-list, .search-input');
            node.on('click', '.choices-list, .search-input', function(e){
                if(e.target != this){
                    return false;
                }

                var _choicesList 	= node.find('.choices-list'),
                _searchField 	= node.find('.search-field');

                var _w  = _choicesList.width()
                    - _searchField.get(0).offsetLeft
                    - 2;
                _searchField.width(_w);
                _searchField.find('input').focus();
            });
        }
        /**
         * 联想兴趣词
         */
        function suggestion(_interest, _locale){
            var param = DATA.body([{key:"interestsList", value:[_interest]},{key:"locale", value:_locale}]);
            http.do("/search/searchInterestSuggestion",param,"GET").then(function(result){
                if(typeof(result)=="string") result = JSON.parse(result);
                var da = result.data;
                renderSuggession(da, _locale)
            },function(err){});
        }

        /**
         * 渲染联想词区域
         */
        function renderSuggession(_suggestionList, _locale){
            var _interestsInput =  node.find('[data-type=interests]');
            var choicedInterest = _interestsInput.data("choicedInterest") || {};
            var suggessionC = node.find("div[suggession]");
            suggessionC.removeClass("hide");
            var _suggestion = node.find("ul.suggession-list");
            _suggestion.empty();
            var _html = '';
            if (0 < _suggestionList.length){
                $.each(_suggestionList, function(i, _suggestion){
                    _html += '<li>\
                    <a href="javascript:void(0);" node-id="' + _suggestion.id + '" title="'+ _suggestion.name + '" interest-id="'+ _suggestion.id + '" interest-name="' + _suggestion.name + '"\
                    locale="' + _locale + '" class="suggession-btn choices-action btn btn-default txt-color-dark' +  (choicedInterest[_suggestion.id] ? ' is-checked' : '') + '">\
                    <span><strong>' + _suggestion.name + '</strong></span>\
                    </br>\
                    <span style="font-size:12px">' + kit.toThousands(_suggestion.audienceSize || 0) + '</span>\
                    </a>\
                    </li>';
                });
            } else {
                _html = '<li>'+I18N.getText('common', 'resultEmpty')+'...</li>';
            }

            _suggestion.append(_html);
        }

        /**
         * 点击其他区域 兴趣搜素恢复初始状态
         */
        function recoverInterestAreaEvent(){
            //点击其他区域 兴趣搜素恢复初始状态
            $("body").off('mouseup').bind('mouseup', function(e){
                var currentIndex = $(e.target).parents('.interest').get(0);
                var allInterestArea = $(".interest");
                $.each(allInterestArea, function(i, _iInterestArea){
                    //当前点击源处于兴趣节点内时，不进行关闭操作
                    if (currentIndex != $(_iInterestArea).get(0)){
                        //清空输入框
                        $(_iInterestArea).find('[data-type=interests]').val('');

                        $(_iInterestArea).find('div[search]').addClass("hide");
                        $(_iInterestArea).find('.choices-area').removeClass('seach-active');
                        $(_iInterestArea).find('div[browse]').addClass("hide");
                        $(_iInterestArea).find('div[suggession]').addClass("hide");
                    }
                });

            });
        }

        function fillCache(){
            $.each(data, function(i, _data){
                choiceInterest(_data.id, _data.name, _data.locale, false)
            });
        }
    }
});
